{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from abc import abstractmethod,ABCMeta\n",
    "import random\n",
    "class sort(metaclass=ABCMeta):\n",
    "    @abstractmethod\n",
    "    def sort(self,data):\n",
    "        pass\n",
    "class quicksort(sort):\n",
    "    def quick_sort(self,data,left,right):\n",
    "        if left<right:\n",
    "            mid=self.partition(data,left,right)\n",
    "            self.quick_sort(data,left,right)\n",
    "            self.quick_sort(data,mid+1,right)\n",
    "    def partition(self,data,left,right):\n",
    "        tmp=data[left]\n",
    "        while left<right:\n",
    "            while left<right and data[right]>=tmp:\n",
    "                right-=1\n",
    "            data[left]=data[right]\n",
    "            while left<right and data[ldft]<=tmp:\n",
    "                left+=1\n",
    "            data[right]=data[left]\n",
    "        data[left]=tmp\n",
    "        return tmp\n",
    "    def sort(self,data):\n",
    "        print('快速排序')\n",
    "        return self.quick_sort(data,0,len(data)-1)\n",
    "class mergesort(sort):\n",
    "    def merge(self,data,low,mid,high):\n",
    "        i=low\n",
    "        j=mid+1\n",
    "        ltmp=[]\n",
    "        while i<=mid and j<=high:\n",
    "            if data[i]<=data[j]:\n",
    "                ltmp.append(data[i])\n",
    "                i+=1\n",
    "            else:\n",
    "                ltmp.append(data[j])\n",
    "                j+=1\n",
    "        while i<=mid:\n",
    "            ltmp.append(data[i])\n",
    "            i+=1\n",
    "        \n",
    "        while j<=high:\n",
    "            ltmp.append(data[j])\n",
    "            j+=1\n",
    "        data[low:high +1]=ltmp\n",
    "    def merge_sort(self,data,low,high):\n",
    "        if low<high:\n",
    "            mid=(low+high)//2\n",
    "            self.merge_sort(data,low,mid)\n",
    "            self.merge_sort(data,mid+1,high)\n",
    "            self.merge(data,low,mid,high)\n",
    "    def sort(self,data):\n",
    "        print('归并排序')\n",
    "        return self.merge_sort(data,0,len(data)-1)\n",
    "class context:\n",
    "    def __init__(self,data,strategy=None):\n",
    "        self.data=data\n",
    "        self.strategy=strategy\n",
    "    def set_strategy(self,strategy):\n",
    "        self.strategy=strategy\n",
    "    def do_strategy(self):\n",
    "        if self.strategy:\n",
    "            self.strategy.sort(self.data)\n",
    "        else:\n",
    "            raise TypeError\n",
    "li=list(range(100000))\n",
    "random.shuffle(li)\n",
    "\n",
    "context=context(li,mergesort())\n",
    "context.do_strategy()\n",
    "\n",
    "random.shuffle(conntext.data)\n",
    "\n",
    "context.set_strategy(quicksort())\n",
    "context.do_strategy()\n",
    "                "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
